package cn.ultrarisk.scadmin.manager.biz.monitor.dao;

import cn.ultrarisk.mybatis.extension.query.Query;
import cn.ultrarisk.scadmin.manager.biz.monitor.entity.SolrNodeMonitor;
import cn.ultrarisk.scadmin.manager.support.BaseDAO;
import cn.ultrarisk.scadmin.manager.util.CollectionUtil;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * solr节点监控数据操作类
 *
 * @author sevendlong(lin23871@163.com)
 * @date 2014/3/28
 * @since V1.0
 */
@Repository
public class SolrNodeMonitorDAO extends BaseDAO<SolrNodeMonitor, Long> {
    /**
     * 构造器
     */
    public SolrNodeMonitorDAO() {
        super(SolrNodeMonitor.class, SolrNodeMonitor.COLUMN_PROPERTY_MAP);
    }

    public List<Integer> groupByNodeId(long startTime, long endTime) {
        Query query = Query.gteQuery(SolrNodeMonitor.CREATE_TIME, startTime).lt(SolrNodeMonitor.CREATE_TIME, endTime);
        query.addGroup(SolrNodeMonitor.NODE_ID);
        return findColumn(query, SolrNodeMonitor.NODE_ID, true, true);
    }

    public Map<Integer, Map<String, Long>> getMemStat(long startTime, long endTime) {
        List<Integer> nodeIds = groupByNodeId(startTime, endTime);

        if (CollectionUtil.isEmpty(nodeIds)) {
            return null;
        }

        Query query = Query.gteQuery(SolrNodeMonitor.CREATE_TIME, startTime).lt(SolrNodeMonitor.CREATE_TIME, endTime);
        query.addAttach("nodeIds", nodeIds);
        Map<String, Object> memStat = sqlSessionTemplate.selectOne(entityName + ".memStat", query.toMap(tableName));

        Map<Integer, Map<String, Long>> stat = new HashMap<Integer, Map<String, Long>>();
        for (String key : memStat.keySet()) {
            Integer nodeId = Integer.parseInt(key.substring(4));
            Long value = Math.round(Double.parseDouble(memStat.get(key).toString()));
            if (stat.get(nodeId) == null) {
                stat.put(nodeId, new HashMap<String, Long>());
            }
            stat.get(nodeId).put((key.startsWith("max") ? "max" : "avg"), value);
        }
        return stat;
    }

    public Map<Integer, Map<String, Double>> getLoadStat(long startTime, long endTime) {
        List<Integer> nodeIds = groupByNodeId(startTime, endTime);

        if (CollectionUtil.isEmpty(nodeIds)) {
            return null;
        }
        Query query = Query.gteQuery(SolrNodeMonitor.CREATE_TIME, startTime).lt(SolrNodeMonitor.CREATE_TIME, endTime);
        query.addAttach("nodeIds", nodeIds);
        Map<String, Object> memStat = sqlSessionTemplate.selectOne(entityName + ".loadStat", query.toMap(tableName));

        Map<Integer, Map<String, Double>> stat = new HashMap<Integer, Map<String, Double>>();
        for (String key : memStat.keySet()) {
            Integer nodeId = Integer.parseInt(key.substring(4));
            Double value = Double.parseDouble(memStat.get(key).toString());
            if (stat.get(nodeId) == null) {
                stat.put(nodeId, new HashMap<String, Double>());
            }
            stat.get(nodeId).put((key.startsWith("max") ? "max" : "avg"), value);
        }
        return stat;
    }

    public Map<Integer, SolrNodeMonitor> getNewest(List<Integer> nodeIds) {
        if (CollectionUtil.isEmpty(nodeIds)) {
            return new HashMap<Integer, SolrNodeMonitor>();
        }
        Query query = Query.inQuery(SolrNodeMonitor.NODE_ID, nodeIds);
        List<SolrNodeMonitor> monitors = sqlSessionTemplate.selectList(entityName + ".getNewest", query.toMap(tableName));
        Map<Integer, SolrNodeMonitor> monitorMap = new HashMap<Integer, SolrNodeMonitor>();
        for (SolrNodeMonitor monitor : monitors) {
            monitorMap.put(monitor.getNodeId(), monitor);
        }
        return monitorMap;
    }
}
